#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <curl/curl.h>
#include "Batu.h"

string data_collected;

size_t GetContent(char *buf, size_t size, size_t nmemb)
{
    data_collected.append(buf);
    return nmemb;
}

size_t CreateFile(char *buf, size_t size, size_t nmemb, void *stream)
{
    ofstream *out = (ofstream*) stream;
    out->write(buf, nmemb);
    return nmemb;
}

Batu::Batu(string& tags, char* page_id)
{
    source_page_url = "http://gelbooru.com/index.php?page=post&s=list&tags=" + tags + "&pid=" + page_id;
    num_of_target = 0;
}

Batu::~Batu()
{

}

void Batu::FetchPageContent(string& url, string &target)
{
    CURL *curl;
    CURLcode retval;
    
    data_collected.clear();

    curl = curl_easy_init();
    if (curl)
    {
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, GetContent);

        retval = curl_easy_perform(curl);

        if (retval != CURLE_OK)
        {
            cerr << "error: curl_easy_perform failed: " << curl_easy_strerror(retval) << endl;
            exit(0);
        }
        else target = data_collected;

        curl_easy_cleanup(curl);
    }
}

void Batu::GetPic()
{
    CURL *curl;
    CURLcode retval;
    ofstream fout;
    string file_name;
    int name_start;

    for(int i = 0; i < num_of_target; i++)
    {
        name_start = target_url[i].find_last_of("/");
        file_name = target_url[i].substr(name_start + 1, target_url[i].length() - 1);

        fout.open(file_name.c_str(), ios::app);

        curl = curl_easy_init();
        if (curl)
        {
            curl_easy_setopt(curl, CURLOPT_URL, target_url[i].c_str());
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CreateFile);
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fout);

            retval = curl_easy_perform(curl);

            if (retval != CURLE_OK)
            {
                cerr << "error: curl_easy_perform failed: " << curl_easy_strerror(retval) << endl;
                exit(0);
            }
            curl_easy_cleanup(curl);
        }

        if (fout.is_open())
            fout.close();
    }
}

void Batu::GetSourcePageURL()
{
    cout << "Input the source page: " << endl;
    cin >> source_page_url;
}

void Batu::GetSourcePageContent()
{
    FetchPageContent(source_page_url, source_page_content);
    //cout << source_page_content << endl;
}

bool Batu::GetTargetPageURLAndPreviewURL()
{
    int find, url_start, url_end/*, pre_start, pre_end*/;
    bool flag = false;
    find = 0;

    while (1)
    {
        find = source_page_content.find("class=\"thumb\"", find);
        if (find != -1)
        {
            flag = true;
            url_start = find + 21;
            url_end = source_page_content.find("\"", url_start + 1);
            target_page_url[num_of_target] = "http://gelbooru.com/index.php?page=post&s=view&id="
                    + source_page_content.substr(url_start + 1, url_end - url_start - 1);
            //pre_start = source_page_content.find("http", url_end);
            //pre_end = source_page_content.find("\"", pre_start);
            //target_preview_url[num_of_target] = source_page_content.substr(pre_start, pre_end - pre_start);
            find++;
            num_of_target++;
        }
        else break;
    }
    
    if(flag)
        return true;
    else return false;
}

int Batu::GetNumOfTarget()
{
    return num_of_target;
}

void Batu::GetTargetPageContent()
{
    for(int i = 0; i < num_of_target; i++)
        FetchPageContent(target_page_url[i], target_page_content[i]);
    //cout << target_page_content[val] << endl;
}

void Batu::GetTargetURL()
{
    int find, start_pos, end_pos;
    find = 0;

    for(int i = 0; i < num_of_target; i++)
    {
        find = target_page_content[i].find("Option");
        start_pos = target_page_content[i].find("http", find + 1);
        end_pos = target_page_content[i].find("\"", start_pos + 1);
        target_url[i] = target_page_content[i].substr(start_pos, end_pos - start_pos);
    }
    //cout << target_url[val] << endl;
}